package com.example.yuejian23.yuejian_23.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.yuejian23.yuejian_23.Fragment.HomeFragment;
import com.example.yuejian23.yuejian_23.Fragment.ShaituFragment;
import com.example.yuejian23.yuejian_23.Fragment.ProFragment;
import com.example.yuejian23.yuejian_23.Fragment.JulebuFragment;
import com.example.yuejian23.yuejian_23.Fragment.PeixunFragment;
import com.example.yuejian23.yuejian_23.R;

public class MainActivity extends FragmentActivity implements OnClickListener
{
    private LinearLayout mTabJianshen;
    private LinearLayout mTabShaitu;
    private LinearLayout mTabZhuye;
    private LinearLayout mTabJulebu;
    private LinearLayout mTabPro;

    private ImageButton mImgJianshen;
    private ImageButton mImgShaitu;
    private ImageButton mImgZhuye;
    private ImageButton mImgJulebu;
    private ImageButton mImgPro;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;

    ImageView tu,sou;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setSelect(2);
    }

    private void initEvent()
    {
        mTabJianshen.setOnClickListener(this);
        mTabShaitu.setOnClickListener(this);
        mTabZhuye.setOnClickListener(this);
        mTabJulebu.setOnClickListener(this);
        mTabPro.setOnClickListener(this);
    }

    private void initView()
    {
        mTabJianshen = (LinearLayout) findViewById(R.id.id_tab_jianshen);
        mTabShaitu = (LinearLayout) findViewById(R.id.id_tab_shaitu);
        mTabZhuye = (LinearLayout) findViewById(R.id.id_tab_zhuye);
        mTabJulebu = (LinearLayout) findViewById(R.id.id_tab_julebu);
        mTabPro = (LinearLayout) findViewById(R.id.id_tab_pro);

        mImgJianshen = (ImageButton) findViewById(R.id.id_tab_jianshen_img);
        mImgShaitu = (ImageButton) findViewById(R.id.id_tab_shaitu_img);
        mImgZhuye = (ImageButton) findViewById(R.id.id_tab_zhuye_img);
        mImgJulebu = (ImageButton) findViewById(R.id.id_tab_julebu_img);
        mImgPro = (ImageButton) findViewById(R.id.id_tab_pro_img);

        sou = (ImageView) findViewById(R.id.sou);
        sou.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,SousuoActivity.class);
                startActivity(intent);
            }
        });
        tu = (ImageView) findViewById(R.id.tu);
        tu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,FatieActivity.class);
                startActivity(intent);
            }
        });//跳转
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new PeixunFragment();
                    transaction.add(R.id.id_content, mTab01);
                    tu.setVisibility(View.GONE);
                    sou.setVisibility(View.VISIBLE);
                } else
                {
                    transaction.show(mTab01);
                    tu.setVisibility(View.GONE);
                    sou.setVisibility(View.VISIBLE);
                }
                mImgJianshen.setImageResource(R.drawable.ic_peixunhei);
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new ShaituFragment();transaction.add(R.id.id_content, mTab02);
                    tu.setVisibility(View.VISIBLE);
                    sou.setVisibility(View.GONE);
                } else
                {
                    transaction.show(mTab02);
                    tu.setVisibility(View.VISIBLE);
                    sou.setVisibility(View.GONE);

                }
                mImgShaitu.setImageResource(R.drawable.ic_shaituhei);
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new HomeFragment();
                    transaction.add(R.id.id_content, mTab03);
                    tu.setVisibility(View.GONE);
                    sou.setVisibility(View.GONE);
                } else
                {
                    transaction.show(mTab03);
                    tu.setVisibility(View.GONE);
                    sou.setVisibility(View.GONE);
                }
                mImgZhuye.setImageResource(R.drawable.ic_zhuyehei);
                break;
            case 3:
                if (mTab04 == null)
                {
                    mTab04 = new JulebuFragment();
                    transaction.add(R.id.id_content, mTab04);
                    tu.setVisibility(View.GONE);
                    sou.setVisibility(View.GONE);
                } else
                {
                    transaction.show(mTab04);
                    tu.setVisibility(View.GONE);
                    sou.setVisibility(View.GONE);
                }
                mImgJulebu.setImageResource(R.drawable.ic_julebuhei);
                break;
            case 4:
            if (mTab05 == null)
            {
                mTab05 = new ProFragment();
                transaction.add(R.id.id_content, mTab05);
                tu.setVisibility(View.GONE);
                sou.setVisibility(View.GONE);
            } else
            {
                transaction.show(mTab05);
                tu.setVisibility(View.GONE);
                sou.setVisibility(View.GONE);
            }
            mImgPro.setImageResource(R.drawable.ic_gerenhei);
            break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_tab_jianshen:
                setSelect(0);
                break;
            case R.id.id_tab_shaitu:
                setSelect(1);
                break;
            case R.id.id_tab_zhuye:
                setSelect(2);
                break;
            case R.id.id_tab_julebu:
                setSelect(3);
                break;
            case R.id.id_tab_pro:
                setSelect(4);
                break;

            default:
                break;
        }
    }


    private void resetImgs()
    {
        mImgJianshen.setImageResource(R.drawable.ic_peixunbai);
        mImgShaitu.setImageResource(R.drawable.ic_shaitubai);
        mImgZhuye.setImageResource(R.drawable.ic_zhuyebai);
        mImgJulebu.setImageResource(R.drawable.ic_julebubai);
        mImgPro.setImageResource(R.drawable.ic_gerenbai);
    }

}
